/**
 * 
 */
package yuanzw.movie.dao.impl;

import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import yuanzw.movie.dao.UserDao;
import yuanzw.movie.domain.User;
import yuanzw.movie.utils.HibernateUtil;

/**
* @author yuanzhongwei
* @version 创建时间：2017年10月8日 上午10:41:14
*/
/**
 * @author yuanzhongwei
 *
 */
public class UserDaoHibernateImpl implements UserDao{

	/* (non-Javadoc)
	 * @see yuanzw.movie.dao.UserDao#savaUser(yuanzw.movie.domain.User)
	 */
	public void savaUser(User user) {
		// TODO Auto-generated method stub
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtil.getSession();
			tx = s.beginTransaction();
			s.save(user);
			tx.commit();
		}finally {
			if(s != null) {
				s.close();
			}
		}
	}

	/* (non-Javadoc)
	 * @see yuanzw.movie.dao.UserDao#findUserByName(java.lang.String)
	 */
	@SuppressWarnings("deprecation")
	public User findUserByName(String name) {
		// TODO Auto-generated method stub
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			Criteria c = s.createCriteria(User.class);
			c.add(Restrictions.eq("name", name));
			User user = (User) c.uniqueResult();
			return user;
		}finally {
			if(s != null) {
				s.close();
			}
		}
	}

	/* (non-Javadoc)
	 * @see yuanzw.movie.dao.UserDao#findUserById(int)
	 */
	public User findUserById(int id) {
		// TODO Auto-generated method stub
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			User user = s.get(User.class, id);
			return user;
		}finally {
			if(s != null) {
				s.close();
			}
		}
	}

	/* (non-Javadoc)
	 * @see yuanzw.movie.dao.UserDao#updateUser(yuanzw.movie.domain.User)
	 */
	public void updateUser(User user) {
		// TODO Auto-generated method stub
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtil.getSession();
			tx = s.beginTransaction();
			s.update(user);
			tx.commit();
		}finally {
			if(s != null) {
				s.close();
			}
		}
		
	}

	/* (non-Javadoc)
	 * @see yuanzw.movie.dao.UserDao#remove(yuanzw.movie.domain.User)
	 */
	public void remove(User user) {
		// TODO Auto-generated method stub
        Session s = null;
        Transaction tx = null;
        try {
            s = HibernateUtil.getSession();
            tx = s.beginTransaction();
            s.delete(user);
            tx.commit();
        } finally {
            if(s != null)
                s.close();
        }
    }
}
